Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Listing 10.5 shows the modified code to handle the DataTruncation.

Listing 10.5 Processing DataTruncation warnings.

// Call fooBar to create a warning chain
fooBar();

// Now, poll for the warning chain. We'll simply dump any warning
// messages to standard output.
SQLWarning chain = getWarnings();

if (chain   != null) {
         System.out.println("Warning(s):");

    // Display the chain until no more entries exist
    while (chain != null) {
        // The only way we can tell if this warning is a DataTruncation
         // is to attempt to cast it. This may fail, indicating that
        // it is just an SQLWarning.
        try {
              DataTruncation trunc = (DataTruncation) chain;
              System.out.println("Data Truncation on column: " +
                  trunc.getIndex());
       }
        catch (Exception ex) {
              System.out.println("Message: " + chain.getMessage());
       }

        // Advance to the next warning in the chain. null will be
        // returned if no more entries exist.
        chain = chain.getNextWarning();
    }
}

JDBC Data Types

The JDBC specification provides definitions for all of the SQL data types that can be supported by a JDBC driver. Only a few of these data types may be natively supported by a given database system, which is why data coercion becomes such a vital service (we’ll discuss data coercion a little later in this chapter). The data types are defined in Types.class:

public class Types
{

    public final static int BIT = -7;
    public final static int TINYINT = -6;
    public final static int SMALLINT = 5;
    public final static int INTEGER = 4;
    public final static int BIGINT = -5;
    public final static int FLOAT = 6;
    public final static int REAL = 7;
    public final static int DOUBLE = 8;
    public final static int NUMERIC = 2;
    public final static int DECIMAL = 3;
    public final static int CHAR = 1;
    public final static int VARCHAR = 12;
    public final static int LONGVARCHAR = -1;
    public final static int DATE = 91;
    public final static int TIME = 92;
    public final static int TIMESTAMP = 93;
    public final static int BINARY = -2;
    public final static int VARBINARY = -3;
    public final static int LONGVARBINARY = -4;
    public final static int OTHER = 1111;
}

At a minimum, a JDBC driver must support one (if not all) of the character data types (CHAR, VARCHAR, and LONGVARCHAR). A driver may also support driver-specific data types (OTHER) which can only be accessed in a JDBC application as an Object. In other words, you can get data as some type of object and put it back into a database as that same type of object, but the application has no idea what type of data is actually contained within. Let’s take a look at each of the data types more closely.

Character Data: CHAR, VARCHAR, And LONGVARCHAR

CHAR, VARCHAR, and LONGVARCHAR data types are used to express character data. These data types are represented in JDBC as Java String objects. Data of type CHAR is represented as a fixed-length String, and may include some padding spaces to ensure that it is the proper length. If data is being written to a database, the driver must ensure that the data is properly padded. Data of type VARCHAR is represented as a variable-length String, and is trimmed to the actual length of the data. LONGVARCHAR data can be either a variable-length String or returned by the driver as a Java InputStream, allowing the data to be read in chunks of whatever size the application desires.

Exact Numeric Data: NUMERIC And DECIMAL

The NUMERIC and DECIMAL data types are used to express signed, exact numeric values with a fixed number of decimal places. These data types are often used to represent currency values. NUMERIC and DECIMAL data are both represented in JDBC as Numeric objects. The Numeric class is new with JDBC, and we’ll be discussing it shortly.

Binary Data: BINARY, VARBINARY, And LONGVARBINARY

The BINARY, VARBINARY, and LONGVARBINARY data types are used to express binary (non-character) data. These data types are represented in JDBC as Java byte arrays. Data of type BINARY is represented as a fixed-length byte array, and may include some padding zeros to ensure that it is the proper length. If data is being written to a database, the driver must ensure that the data is properly padded. Data of type VARBINARY is represented as a variable-length byte array, and is trimmed to the actual length of the data. LONGVARBINARY data can either be a variable-length byte array or returned by the driver as a Java InputStream, allowing the data to be read in chunks of whatever size the application desires.

Boolean Data: BIT

The BIT data type is used to represent a boolean value—either true or false—and is represented in JDBC as a Boolean object or boolean data type.

Integer Data: TINYINT, SMALLINT, INTEGER, And BIGINT

The TINYINT, SMALLINT, INTEGER, and BIGINT data types are used to represent signed integer data. Data of type TINYINT is represented in JDBC as a Java byte data type (1 byte), with a minimum value of -128 and a maximum value of 127. Data of type SMALLINT is represented in JDBC as a Java short data type (2 bytes), with a minimum value of -32,768 and a maximum value of 32,767. Data of type INTEGER is represented as a Java int data type (4 bytes), with a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. Data of type BIGINT is represented as a Java long data type (8 bytes), with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807.

Floating-Point Data: REAL, FLOAT, And DOUBLE

The REAL, FLOAT, and DOUBLE data types are used to represent signed, approximate values. Data of type REAL supports seven digits of mantissa precision, and is represented as a Java float data type. Data of types FLOAT and DOUBLE support 15 digits of mantissa precision, and are represented as Java double data types.


Previous Table of Contents Next